<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>自定义 _all 字段 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/custom-all.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/custom-all.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-all.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-all.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="multi-field-search.html">多字段搜索</a></span>
»
<span class="breadcrumb-node">自定义 _all 字段</span>
</div>
<div class="navheader">
<span class="prev">
<a href="field-centric.html">« 字段中心式查询</a>
</span>
<span class="next">
<a href="_cross_fields_queries.html">cross-fields 跨字段查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="custom-all"></a>自定义 _all 字段<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/45_Custom_all.asciidoc">edit</a>
</h2>
</div></div></div>
<p>在 <a class="xref" href="root-object.html#all-field" title="元数据: _all 字段">all-field</a> 字段中，我们解释过 <code class="literal">_all</code> 字段的索引方式是将所有其他字段的值作为一个大字符串索引的。然而这么做并不十分灵活，为了灵活我们可以给人名添加一个自定义 <code class="literal">_all</code> 字段，再为地址添加另一个 <code class="literal">_all</code> 字段。</p>
<p>Elasticsearch 在字段映射中为我们提供 <code class="literal">copy_to</code> 参数来实现这个功能：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">PUT /my_index
{
    "mappings": {
        "person": {
            "properties": {
                "first_name": {
                    "type":     "string",
                    "copy_to":  "full_name" <a id="CO76-1"></a><i class="conum" data-value="1"></i>
                },
                "last_name": {
                    "type":     "string",
                    "copy_to":  "full_name" <a id="CO76-2"></a><i class="conum" data-value="1"></i>
                },
                "full_name": {
                    "type":     "string"
                }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/110_Multi_Field_Search/45_Custom_all.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO76-1"><i class="conum" data-value="1"></i></a><a href="#CO76-2"></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">first_name</code> 和 <code class="literal">last_name</code> 字段中的值会被复制到 <code class="literal">full_name</code> 字段。</p>
</td>
</tr>
</table>
</div>
<p>有了这个映射，我们可以用 <code class="literal">first_name</code> 来查询名，用 <code class="literal">last_name</code> 来查询姓，或者直接使用 <code class="literal">full_name</code> 查询整个姓名。</p>
<p><code class="literal">first_name</code> 和 <code class="literal">last_name</code> 的映射并不影响 <code class="literal">full_name</code> 如何被索引， <code class="literal">full_name</code> 将两个字段的内容复制到本地，然后根据 <code class="literal">full_name</code> 的映射自行索引。</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">copy_to</code> 设置对<a class="xref" href="multi-fields.html" title="字符串排序与多字段">multi-field</a>无效。如果尝试这样配置映射，Elasticsearch 会抛异常。</p>
<p>为什么呢？多字段只是以不同方式简单索引“主”字段；它们没有自己的数据源。也就是说没有可供 <code class="literal">copy_to</code> 到另一字段的数据源。</p>
<p>只要对“主”字段 <code class="literal">copy_to</code> 就能轻而易举的达到相同的效果：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">PUT /my_index
{
    "mappings": {
        "person": {
            "properties": {
                "first_name": {
                    "type":     "string",
                    "copy_to":  "full_name", <a id="CO77-1"></a><i class="conum" data-value="1"></i>
                    "fields": {
                        "raw": {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                },
                "full_name": {
                    "type":     "string"
                }
            }
        }
    }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO77-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">copy_to</code> 是针对“主”字段，而不是多字段的</p>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="field-centric.html">« 字段中心式查询</a>
</span>
<span class="next">
<a href="_cross_fields_queries.html">cross-fields 跨字段查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>